/**
 * ComfyUI API types
 */

// HTTP API types
export interface QueuePromptRequest {
  prompt: Record<string, unknown>;
  client_id?: string;
  extra_data?: {
    extra_pnginfo?: {
      workflow?: unknown;
    };
  };
}

export interface QueuePromptResponse {
  prompt_id: string;
  number: number;
  node_errors?: Record<string, string>;
}

export interface QueueStatus {
  queue_running: Array<[number, string, Record<string, unknown>]>;
  queue_pending: Array<[number, string, Record<string, unknown>]>;
}

export interface HistoryItem {
  prompt: Array<unknown>;
  outputs: Record<string, {
    images?: Array<{
      filename: string;
      subfolder: string;
      type: string;
    }>;
  }>;
  status: {
    status_str: 'success' | 'error';
    completed: boolean;
    messages: Array<[string, unknown]>;
  };
}

export interface History {
  [promptId: string]: HistoryItem;
}

export interface SystemStats {
  system: {
    os: string;
    python_version: string;
    embedded_python: boolean;
  };
  devices: Array<{
    name: string;
    type: string;
    index: number;
    vram_total: number;
    vram_free: number;
    torch_vram_total: number;
    torch_vram_free: number;
  }>;
}

export interface ObjectInfo {
  [nodeType: string]: {
    input: {
      required?: Record<string, [string, Record<string, unknown>?]>;
      optional?: Record<string, [string, Record<string, unknown>?]>;
    };
    output: string[];
    output_is_list: boolean[];
    output_name: string[];
    name: string;
    display_name: string;
    description: string;
    category: string;
    output_node: boolean;
  };
}

// WebSocket API types
export interface WSClientMessage {
  type: 'auth' | 'subscribe' | 'unsubscribe';
  data?: unknown;
}

export interface WSServerMessage {
  type: 'status' | 'progress' | 'executing' | 'executed' | 'execution_start' | 'execution_success' | 'execution_error' | 'execution_cached';
  data: unknown;
}

export interface WSStatusMessage extends WSServerMessage {
  type: 'status';
  data: {
    status: QueueStatus;
    sid?: string;
  };
}

export interface WSProgressMessage extends WSServerMessage {
  type: 'progress';
  data: {
    value: number;
    max: number;
    node?: string;
    prompt_id: string;
  };
}

export interface WSExecutingMessage extends WSServerMessage {
  type: 'executing';
  data: {
    node: string | null;
    prompt_id: string;
  };
}

export interface WSExecutedMessage extends WSServerMessage {
  type: 'executed';
  data: {
    node: string;
    output: Record<string, unknown>;
    prompt_id: string;
  };
}

export interface WSExecutionStartMessage extends WSServerMessage {
  type: 'execution_start';
  data: {
    prompt_id: string;
  };
}

export interface WSExecutionSuccessMessage extends WSServerMessage {
  type: 'execution_success';
  data: {
    prompt_id: string;
  };
}

export interface WSExecutionErrorMessage extends WSServerMessage {
  type: 'execution_error';
  data: {
    prompt_id: string;
    node_id: string;
    node_type: string;
    executed: string[];
    exception_message: string;
    exception_type: string;
    traceback: string[];
    current_inputs: Record<string, unknown>;
    current_outputs: Record<string, unknown>;
  };
}

export interface WSExecutionCachedMessage extends WSServerMessage {
  type: 'execution_cached';
  data: {
    prompt_id: string;
    nodes: string[];
  };
}

// API client interface
export interface APIClient {
  queuePrompt(request: QueuePromptRequest): Promise<QueuePromptResponse>;
  getQueue(): Promise<QueueStatus>;
  getHistory(promptId?: string): Promise<History>;
  getSystemStats(): Promise<SystemStats>;
  getObjectInfo(): Promise<ObjectInfo>;
  interrupt(): Promise<void>;
  clearQueue(): Promise<void>;
}
